//
//  AddViewController.m
//  iOSDeme
//
//  Created by wxf on 2024/8/25.
//

#import "AddViewController.h"

@interface AddViewController ()


@property(nonatomic,weak) UITextField *text1;
@property(nonatomic,weak) UITextField *text2;
@property(nonatomic,weak) UILabel *result;

@end

@implementation AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UITextField *text1 = [[UITextField alloc] initWithFrame:CGRectMake(0, 200, 50, 40)];
    text1.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:text1];
    self.text1 = text1;
    
    
    UILabel *addView = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 50, 40)];
    //addView.backgroundColor = [UIColor grayColor];
    addView.text = @"+";
    
    [self.view addSubview:addView];
    
    
    UITextField *text2 = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 50, 40)];
    text2.backgroundColor = [UIColor grayColor];
    [self.view addSubview:text2];
    self.text2 = text2;
    
    
    UILabel *equals = [[UILabel alloc] initWithFrame:CGRectMake(150, 200, 50, 40)];
    //addView.backgroundColor = [UIColor grayColor];
    equals.text = @"=";
    
    [self.view addSubview:equals];
    
    
    
    UILabel *result = [[UILabel alloc] initWithFrame:CGRectMake(200, 200, 50, 40)];
    //addView.backgroundColor = [UIColor grayColor];
    result.text = @"";
    
    [self.view addSubview:result];
    self.result = result;
    
    
    //初始化button
    UIButton * addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置位置和大小
    addButton.frame = CGRectMake(20, 300, self.view.frame.size.width - 40, 40);
    //设置背景色
    addButton.backgroundColor = [UIColor orangeColor];
    //设置标题
    [addButton setTitle:@"点我点我" forState:UIControlStateNormal];
    //设置标题颜色
    [addButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    //设置标题字体大小
    addButton.titleLabel.font = [UIFont systemFontOfSize:15];
    //设置点击事件
    [addButton addTarget:self action:@selector(clickSum) forControlEvents:UIControlEventTouchUpInside];
    //将button添加到父self.view上来做显示
    [self.view addSubview:addButton];
}

//手动添加组件-UIButton - end

- (void)clickSum {
    NSLog(@"AddViewController sum");
    NSLog(@"AddViewController view = %@",self.view);
    NSLog(@"AddViewController _text1 = %@",self.text1);
    NSLog(@"AddViewController _text2 = %@",self.text2);
    NSLog(@"AddViewController _result = %@",self.result);
    //获取数字
    NSString *addStr1 = self.text1.text;
    NSString *addStr2 = self.text2.text;
    
    //转换成int
    int num1 = [addStr1 intValue];
    int num2 = [addStr2 intValue];
    
    NSLog(@"ViewController addStr1 = %@  num1=%d",addStr1,num1);
    NSLog(@"ViewController addStr2 = %@  num2=%d",addStr2,num2);
    
    //相加
    int sum = num1+num2;
    
    //设置结果
    self.result.text = [NSString stringWithFormat:@"%d", sum];
    
    //唤醒键盘的控件 - 第一响应者
    //UITextField - 默认可以唤醒键盘 拒绝做第一响应者就可以把键盘退下
    [self.text2 resignFirstResponder];
}


@end
